﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FastCodeGeneration.Utility
{
    public static class StringUtility
    {
        public static string ToCamelToUnderline(this string str)
        {
            var rgx = @"(?x)( [A-Z][a-z,0-9]+ | [A-Z]+(?![a-z]) )";

            return Regex.Replace(str, rgx, "_$0").ToLower().TrimStart('_');
        }

        /// <summary>
        /// 将大驼峰命名转为蛇形命名 CoderBusy  coderBusy
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToLowerCamelCase(this string str)
        {
            var firstChar = str[0];
            if (firstChar == char.ToLowerInvariant(firstChar))
            {
                return str;
            }
            var name = str.ToCharArray();
            name[0] = char.ToLowerInvariant(firstChar);
            return new String(name);
        }

        /// <summary>
        /// 将大驼峰命名转为下划线   CoderBusy  coder_busy
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string CamelToUnderline(this string str)
        {
            var builder = new StringBuilder();
            var name = str;
            var previousUpper = false;

            for (var i = 0; i < name.Length; i++)
            {
                var c = name[i];
                if (char.IsUpper(c))
                {
                    if (i > 0 && !previousUpper)
                    {
                        builder.Append("_");
                    }
                    builder.Append(char.ToLowerInvariant(c));
                    previousUpper = true;
                }
                else
                {
                    builder.Append(c);
                    previousUpper = false;
                }
            }
            return builder.ToString();
        }

        public static string LineToHump(this string name)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var s in name.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries))
            {
                builder.Append(Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s));
            }

            return builder.ToString();
        }


        public static bool IsEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        public static bool IsNotEmpty(this string str)
        {
            return !str.IsEmpty();
        }

        public static bool IsBlank(this string str)
        {
            return str == null || str.Trim().Length == 0;
        }

        public static bool IsNotBlank(this string str)
        {
            return !str.IsBlank();
        }

    }
}
